package cn.lingyangwl.agile.infra;

import cn.lingyangwl.agile.infra.common.ScanScene;
import cn.lingyangwl.agile.infra.datamanage.service.*;
import cn.lingyangwl.agile.infra.file.service.FileRecordService;
import cn.lingyangwl.agile.infra.file.service.MediaMaterialService;
import cn.lingyangwl.agile.infra.group.service.DataGroupService;
import cn.lingyangwl.agile.infra.idgenerator.AutoRegisterWorkerId;
import cn.lingyangwl.agile.infra.misc.service.*;
import cn.lingyangwl.agile.infra.module.datamanage.db.*;
import cn.lingyangwl.agile.infra.module.file.FileProperties;
import cn.lingyangwl.agile.infra.module.file.constants.FileCons;
import cn.lingyangwl.agile.infra.module.file.model.rqrs.FileRecordResp;
import cn.lingyangwl.agile.infra.module.file.model.rqrs.MediaMaterialResp;
import cn.lingyangwl.agile.infra.module.group.model.rqrs.*;
import cn.lingyangwl.agile.infra.module.misc.*;
import cn.lingyangwl.agile.infra.weixin.mp.handler.WxMpHandler;
import cn.lingyangwl.agile.model.enums.DataGroupBizTypeEnum;
import cn.lingyangwl.framework.tool.core.UrlUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author shenguangyang
 */
@Primary
@Service
public class InfraApiImpl implements InfraApi {
    @Resource
    private DataGroupService groupService;
    @Resource
    private FileProperties fileProperties;
    @Resource
    private FileRecordService fileRecordService;
    @Resource
    @Lazy
    private WxMpHandler wxMpHandler;
    @Resource
    private MediaMaterialService materialService;
    @Resource
    private AutoRegisterWorkerId autoRegisterWorkerId;
    @Resource
    private DatasourceService datasourceService;
    @Resource
    private InfraCommonService infraCommonService;

    @Override
    public MediaMaterialResp getMediaMaterial(Long id) {
        return materialService.getMaterial(id);
    }

    @Override
    public Long saveDataGroup(DataGroupSaveReq req) {
        return groupService.saveGroup(req);
    }

    @Override
    public List<DataGroupDto> listChildrenDataGroup(Long parentId) {
        return groupService.listChildrenGroup(parentId);
    }

    @Override
    public List<DataGroupDto> listDataGroup(DataGroupQuery req) {
        return groupService.listGroup(req);
    }

    @Override
    public boolean updateDataGroup(DataGroupUpdateReq req) {
        return groupService.updateGroup(req);
    }

    @Override
    public DataGroupDto getDataGroup(Long id) {
        return groupService.getGroup(id);
    }

    @Override
    public boolean deleteDataGroup(DataGroupBizTypeEnum type, Long id) {
        DataGroupDeleteReq req = new DataGroupDeleteReq();
        req.setBizType(type.getType());
        req.setIds(Collections.singletonList(id));
        return groupService.deleteGroup(req);
    }

    @Override
    public boolean batchDeleteDataGroup(DataGroupDeleteReq req) {
        return groupService.deleteGroup(req);
    }

    @Override
    public String getFileIntranetUrl(String filePath) {
        filePath = filePath.replace(FileCons.UPLOAD_BASE_URL, "");
        return UrlUtils.removeRepeatSlashOfUrl(fileProperties.getIntranetUrl() + "/" + filePath);
    }

    @Override
    public FileRecordResp getFileRecordByPath(String path, boolean isIntranet) {
        return fileRecordService.getRecordByPath(path, isIntranet);
    }

    @Override
    public void deleteFileRecordByPath(String path) {
        fileRecordService.deleteRecordByPath(path);
    }

    @Override
    public String getMpQrcode(ScanScene req) {
        return wxMpHandler.getQrcode(req);
    }

    @Override
    public Short getIdGenNextWorkId() {
        return autoRegisterWorkerId.getNextWorkId();
    }

    @Override
    public void refreshIdGenWorkId(Short workId) {
        autoRegisterWorkerId.refresh(String.valueOf(workId));
    }

    @Override
    public List<SQLContext> listSqlContext(Long datasourceId, List<String> tableNames) {
        return datasourceService.listSqlContext(datasourceId, tableNames);
    }

    @Override
    public Optional<Ip2RegionDto> ip2Region(String ip) {
        return Optional.ofNullable(infraCommonService.ip2Region(ip));
    }
}
